import { getServiceUrl } from '../api';
import RequestService from '../httpRequest';


export default {
    // 获取参数列表
    getParamsList(params, callback, loadingCallback) {
        const queryParams = new URLSearchParams({
            page: params.page,
            limit: params.limit,
            paramCode: params.paramCode
        }).toString();

        RequestService.sendRequest()
            .url(`${getServiceUrl()}/admin/params?${queryParams}`)
            .method('GET')
            .loading(loadingCallback)
            .success((res) => {
                RequestService.clearRequestTime()
                if (res.data) {
                    callback(res.data)
                } else {
                    callback({ code: -1, msg: '服务器响应格式异常' })
                }
            })
            .networkFail((err) => {
                console.error('请求失败:', err)
                RequestService.reAjaxFun(() => {
                    this.getParamsList(params, callback, loadingCallback)
                })
            }).send()
    },

    // 获取参数详情
    getParam(id, callback) {
        RequestService.sendRequest()
            .url(`${getServiceUrl()}/admin/params/${id}`)
            .method('GET')
            .success((res) => {
                RequestService.clearRequestTime()
                if (res.data) {
                    callback(res.data)
                } else {
                    callback({ code: -1, msg: '服务器响应格式异常' })
                }
            })
            .networkFail((err) => {
                console.error('请求失败:', err)
                RequestService.reAjaxFun(() => {
                    this.getParam(id, callback)
                })
            }).send()
    },

    // 保存参数
    saveParam(data, callback) {
        RequestService.sendRequest()
            .url(`${getServiceUrl()}/admin/params`)
            .method('POST')
            .data(data)
            .success((res) => {
                RequestService.clearRequestTime()
                if (res.data) {
                    callback(res.data)
                } else {
                    callback({ code: -1, msg: '服务器响应格式异常' })
                }
            })
            .networkFail((err) => {
                console.error('保存失败:', err)
                RequestService.reAjaxFun(() => {
                    this.saveParam(data, callback)
                })
            }).send()
    },

    // 更新参数
    updateParam(data, callback) {
        RequestService.sendRequest()
            .url(`${getServiceUrl()}/admin/params`)
            .method('PUT')
            .data(data)
            .success((res) => {
                RequestService.clearRequestTime()
                if (res.data) {
                    callback(res.data)
                } else {
                    callback({ code: -1, msg: '服务器响应格式异常' })
                }
            })
            .networkFail((err) => {
                console.error('更新失败:', err)
                RequestService.reAjaxFun(() => {
                    this.updateParam(data, callback)
                })
            }).send()
    },

    // 删除参数
    deleteParam(ids, callback) {
        RequestService.sendRequest()
            .url(`${getServiceUrl()}/admin/params/delete`)
            .method('POST')
            .data(ids)
            .success((res) => {
                RequestService.clearRequestTime()
                if (res.data) {
                    callback(res.data)
                } else {
                    callback({ code: -1, msg: '服务器响应格式异常' })
                }
            })
            .networkFail((err) => {
                console.error('删除失败:', err)
                RequestService.reAjaxFun(() => {
                    this.deleteParam(ids, callback)
                })
            }).send()
    },

    // 用户列表
    getUserList(params, callback) {
        const queryParams = new URLSearchParams({
            page: params.page,
            limit: params.limit,
            mobile: params.mobile
        }).toString();

        RequestService.sendRequest()
            .url(`${getServiceUrl()}/admin/users?${queryParams}`)
            .method('GET')
            .success((res) => {
                RequestService.clearRequestTime()
                if (res.data) {
                    callback(res.data)
                } else {
                    callback({ code: -1, msg: '服务器响应格式异常' })
                }
            })
            .networkFail((err) => {
                console.error('请求失败:', err)
                RequestService.reAjaxFun(() => {
                    this.getUserList(params, callback)
                })
            }).send()
    },

    // 重置用户密码
    resetUserPassword(id, callback) {
        RequestService.sendRequest()
            .url(`${getServiceUrl()}/admin/users/${id}`)
            .method('PUT')
            .success((res) => {
                RequestService.clearRequestTime()
                if (res.data) {
                    callback(res.data)
                } else {
                    callback({ code: -1, msg: '服务器响应格式异常' })
                }
            })
            .networkFail((err) => {
                console.error('重置密码失败:', err)
                RequestService.reAjaxFun(() => {
                    this.resetUserPassword(id, callback)
                })
            }).send()
    },

    // 删除用户
    deleteUser(ids, callback) {
        RequestService.sendRequest()
            .url(`${getServiceUrl()}/admin/users/delete`)
            .method('POST')
            .data(ids)
            .success((res) => {
                RequestService.clearRequestTime()
                if (res.data) {
                    callback(res.data)
                } else {
                    callback({ code: -1, msg: '服务器响应格式异常' })
                }
            })
            .networkFail((err) => {
                console.error('删除用户失败:', err)
                RequestService.reAjaxFun(() => {
                    this.deleteUser(ids, callback)
                })
            }).send()
    },

    // 修改用户状态
    changeUserStatus(status, userIds, callback) {
        RequestService.sendRequest()
            .url(`${getServiceUrl()}/admin/users/changeStatus/${status}`)
            .method('PUT')
            .data(userIds)
            .success((res) => {
                RequestService.clearRequestTime()
                if (res.data) {
                    callback(res.data)
                } else {
                    callback({ code: -1, msg: '服务器响应格式异常' })
                }
            })
            .networkFail((err) => {
                console.error('修改用户状态失败:', err)
                RequestService.reAjaxFun(() => {
                    this.changeUserStatus(status, userIds, callback)
                })
            }).send()
    },

    // 获取API密钥列表
    getApiKeyList(params, callback) {
        const queryParams = new URLSearchParams({
            page: params.page,
            limit: params.limit,
            keyName: params.keyName || ''
        }).toString();

        RequestService.sendRequest()
            .url(`${getServiceUrl()}/admin/apikey/page?${queryParams}`)
            .method('GET')
            .success((res) => {
                RequestService.clearRequestTime()
                if (res.data) {
                    callback(res.data)
                } else {
                    callback({ code: -1, msg: '服务器响应格式异常' })
                }
            })
            .networkFail((err) => {
                console.error('请求失败:', err)
                RequestService.reAjaxFun(() => {
                    this.getApiKeyList(params, callback)
                })
            }).send()
    },

    // 获取API密钥详情
    getApiKey(id, callback) {
        RequestService.sendRequest()
            .url(`${getServiceUrl()}/admin/apikey/${id}`)
            .method('GET')
            .success((res) => {
                RequestService.clearRequestTime()
                if (res.data) {
                    callback(res.data)
                } else {
                    callback({ code: -1, msg: '服务器响应格式异常' })
                }
            })
            .networkFail((err) => {
                console.error('请求失败:', err)
                RequestService.reAjaxFun(() => {
                    this.getApiKey(id, callback)
                })
            }).send()
    },

    // 创建API密钥
    createApiKey(data, callback) {
        RequestService.sendRequest()
            .url(`${getServiceUrl()}/admin/apikey`)
            .method('POST')
            .data(data)
            .success((res) => {
                RequestService.clearRequestTime()
                if (res.data) {
                    callback(res.data)
                } else {
                    callback({ code: -1, msg: '服务器响应格式异常' })
                }
            })
            .networkFail((err) => {
                console.error('创建失败:', err)
                RequestService.reAjaxFun(() => {
                    this.createApiKey(data, callback)
                })
            }).send()
    },

    // 更新API密钥
    updateApiKey(data, callback) {
        RequestService.sendRequest()
            .url(`${getServiceUrl()}/admin/apikey`)
            .method('PUT')
            .data(data)
            .success((res) => {
                RequestService.clearRequestTime()
                if (res.data) {
                    callback(res.data)
                } else {
                    callback({ code: -1, msg: '服务器响应格式异常' })
                }
            })
            .networkFail((err) => {
                console.error('更新失败:', err)
                RequestService.reAjaxFun(() => {
                    this.updateApiKey(data, callback)
                })
            }).send()
    },

    // 删除API密钥
    deleteApiKey(ids, callback) {
        RequestService.sendRequest()
            .url(`${getServiceUrl()}/admin/apikey`)
            .method('DELETE')
            .data(ids)
            .success((res) => {
                RequestService.clearRequestTime()
                if (res.data) {
                    callback(res.data)
                } else {
                    callback({ code: -1, msg: '服务器响应格式异常' })
                }
            })
            .networkFail((err) => {
                console.error('删除失败:', err)
                RequestService.reAjaxFun(() => {
                    this.deleteApiKey(ids, callback)
                })
            }).send()
    },

    // 获取Websocket服务器列表
    getWsServerList(params, callback, loadingCallback) {
        const queryParams = new URLSearchParams(params).toString();
        RequestService.sendRequest()
            .url(`${getServiceUrl()}/admin/server/server-list?${queryParams}`)
            .method('GET')
            .loading(loadingCallback)
            .success((res) => {
                RequestService.clearRequestTime()
                if (res.data) {
                    callback(res.data)
                } else {
                    callback({ code: -1, msg: '服务器响应格式异常' })
                }
            })
            .networkFail((err) => {
                console.error('获取Websocket服务器列表失败:', err)
                RequestService.reAjaxFun(() => {
                    this.getWsServerList(params, callback, loadingCallback)
                })
            }).send()
    },

    // 通知服务端执行操作
    emitServerAction(data, callback) {
        RequestService.sendRequest()
            .url(`${getServiceUrl()}/admin/server/emit-action`)
            .method('POST')
            .data(data)
            .success((res) => {
                RequestService.clearRequestTime()
                if (res.data) {
                    callback(res.data)
                } else {
                    callback({ code: -1, msg: '服务器响应格式异常' })
                }
            })
            .networkFail((err) => {
                console.error('通知服务端操作失败:', err)
                RequestService.reAjaxFun(() => {
                    this.emitServerAction(data, callback)
                })
            }).send()
    }
}